package core

class UserClassController {
	
	
	static layout = 'login'
	static defaultAction = "login"

	def index() {
		
		def max = params.long('max')?:10
		def start = params.long('start')?:0
		[userClass:UserClass.list(max: max, offset:start ),max:max, start:start ]
	}
	
	
	def create()
	{
		
		
	}
	
	def update() {
		def userClass =  UserClass.get(params.long('id'))
		userClass.properties = params
		if(userClass.save()) {
			flash.notice = "User: ${userClass.userName} was updated"
			redirect action:'index'
		}
		else {
			flash.error = "User:  failed to be update"
			render(view:'edit',model:[userClass: UserClass])
		}
	}
	
	def edit()
	{
		[userClass: UserClass.get(params.long('id'))]
	}
	
	def delete()
	{
	def userClass =  UserClass.get(params.id)
	   
		userClass.delete()
		redirect(action:'index')
	}
	
	
	def authenticate(String userName, String passwrd) {
		def user =  UserClass.findByUserNameAndPasswrd("${userName}", "${passwrd}")
		if(user){
			session.user = user
			flash.welcome = "Welcome ${user.userName}"
			redirect (action:'list')
		}
		else {
			flash.loginStatus="login failed: incorrect user name or password"
			redirect(action:'login')
		}
		

	}
	def login(){
		render(view:'Login')
	}
	def save() {
		def user = new UserClass(params)
		
		if (user.save()) {
			flash.notice = "User ${user.userName}: is saved successfuly"
			redirect(action:'create')
		} else {
			flash.error = "User ${user.userName}: Failed to save"
			render view:'create', model:[user:user]
		}
	}
	
	def show() {
		
				[userClass: UserClass.get(params.long('id'))]
			}
	def list () {
		[userClass: UserClass.list()]
	}
}
